package sseGameApps.controller.mdkbr.main;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import sseGameApps.model.mdkbr.MdKbrUser;
import sseGameApps.service.mdkbr.MdKbrUserService;

public class MdFieldResultController extends Controller {


    private MdKbrUserService serviceMdUser = new MdKbrUserService();

    @Override
    public Navigation run() throws Exception {

        int battleCount = 0;
        int winCount = 0;
        int loseCount = 0;
        Long userExp = new Long(0);
        Long eneExp = new Long(0);

        MdKbrUser mdKbrUserP = serviceMdUser.getMdKbrUser(asString("userId"));

        if (mdKbrUserP == null){
            return null;
        }

        // 教官が相手の場合は経験値のみ更新
        if("".equals(asString("eneId")) || "教官".equals(asString("eneId"))){
            if(!"".equals(asString("getUserExp"))){
                if(mdKbrUserP.getUserExp() != null){
                    userExp = Long.parseLong(asString("getUserExp")) + mdKbrUserP.getUserExp();
                } else {
                    userExp = Long.parseLong(asString("getUserExp"));
                }
                mdKbrUserP.setUserExp(userExp);
                mdKbrUserP.setUserLevel(serviceMdUser.checkUserLevel(userExp));
            }
        } else {
            if(mdKbrUserP.getBattleCount() != null){
                battleCount = mdKbrUserP.getBattleCount();
            }
            if(mdKbrUserP.getWinCount() != null){
                winCount = mdKbrUserP.getWinCount();
            }
            if(mdKbrUserP.getLoseCount() != null){
                loseCount = mdKbrUserP.getLoseCount();
            }
            if(!"".equals(asString("getUserExp"))){
                if(mdKbrUserP.getUserExp() != null){
                    userExp = Long.parseLong(asString("getUserExp")) + mdKbrUserP.getUserExp();
                } else {
                    userExp = Long.parseLong(asString("getUserExp"));
                }
                mdKbrUserP.setUserExp(userExp);
                mdKbrUserP.setUserLevel(serviceMdUser.checkUserLevel(userExp));
            }

            mdKbrUserP.setBattleCount(battleCount + 1);

            if("P-WIN".equals(asString("battleResult"))){
                mdKbrUserP.setWinCount(winCount + 1);
            } else if("P-LOSE".equals(asString("battleResult"))){
                mdKbrUserP.setLoseCount(loseCount + 1);
            }
        }

        serviceMdUser.putMdKbrUser(mdKbrUserP);

        if("".equals(asString("eneId")) || "教官".equals(asString("eneId"))){
            return null;
        }

        MdKbrUser mdKbrUserE = serviceMdUser.getMdKbrUser(asString("eneId"));

        if (mdKbrUserE != null){

            if(mdKbrUserE.getBattleCount() == null){
                battleCount = 0;
            } else {
                battleCount = mdKbrUserE.getBattleCount();
            }
            if(mdKbrUserE.getWinCount() == null){
                winCount = 0;
            } else {
                winCount = mdKbrUserE.getWinCount();
            }
            if(mdKbrUserE.getLoseCount() == null){
                loseCount = 0;
            } else {
                loseCount = mdKbrUserE.getLoseCount();
            }
            if(!"".equals(asString("getEneExp"))){
                if(mdKbrUserE.getUserExp() != null){
                    eneExp = Long.parseLong(asString("getEneExp")) + mdKbrUserE.getUserExp();
                } else {
                    eneExp = Long.parseLong(asString("getEneExp"));
                }
                mdKbrUserE.setUserExp(eneExp);
                mdKbrUserE.setUserLevel(serviceMdUser.checkUserLevel(eneExp));
            }

            mdKbrUserE.setBattleCount(battleCount + 1);

            if("P-WIN".equals(asString("battleResult"))){
                mdKbrUserE.setLoseCount(loseCount + 1);
            } else if("P-LOSE".equals(asString("battleResult"))){
                mdKbrUserE.setWinCount(winCount + 1);
            }

            serviceMdUser.putMdKbrUser(mdKbrUserE);
        }

        return null;
    }
}
